package com.skyc.dao.impl;

import java.util.ArrayList;
import java.util.List;
import javax.sql.rowset.CachedRowSet;
import com.skyc.dao.AdminOrderDao;
import com.skyc.dao.ClothesDao;
import com.skyc.dao.UserDao;
import com.skyc.dao.impl.ClothesDaoImpl;
import com.skyc.dao.impl.UserDaoImpl;
import com.skyc.entity.Order;
import com.skyc.util.JDBCUtil;

public class AdminOrderDaoImpl implements AdminOrderDao {
	JDBCUtil util = JDBCUtil.getJDBCUtil();
	UserDao userdao = new UserDaoImpl();
	ClothesDao clothesdao = new ClothesDaoImpl();
	ClothesDao cd = new ClothesDaoImpl();

	@Override
	public List<Order> query() {
		// TODO Auto-generated method stub
		List<Order> list = new ArrayList<Order>();
		String sql = "select *,DATE_FORMAT(o_date,'%Y-%m-%d %H:%i:%s') mytime from t_order ";
		CachedRowSet rs = util.query(sql);
		try {
			while (rs.next()) {
				Order o = new Order();
				o.setOid(rs.getInt("o_id"));
				o.setUid((rs.getInt("u_id")));
				o.setOprice(rs.getInt("o_price"));
				o.setOdate(rs.getString("o_date"));
				o.setOstate(rs.getInt("o_state"));
				o.setOsend(rs.getInt("o_send"));
				o.setOtuihuo(rs.getInt("o_tuihuo"));
				o.setOpayed(rs.getInt("o_payed"));
				o.setOnum(rs.getInt("o_num"));
				o.setOsuccess(rs.getInt("o_success"));
				o.setClothes(cd.queryByID(rs.getInt("c_id") + ""));
				o.setOorder(rs.getInt("o_order"));
				list.add(o);

			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			util.closeCacheRowSet(rs);
		}

		return list;
	}

	@Override
	public boolean deleteByOID(int id) {
		// TODO Auto-generated method stub
		return util.update("DELETE FROM t_order WHERE o_id=?", id);
	}

	@Override
	public boolean updateByOstate(Order order) {
		// TODO Auto-generated method stub

		return util.update("update t_order set o_state=? where o_id=?", order.getOstate(), order.getOid());

	}

	@Override
	public boolean updateByOsend(Order order) {
		// TODO Auto-generated method stub
		return util.update("update t_order set o_send=? where o_id=?", order.getOsend(), order.getOid());
	}

	@Override
	public boolean updateByOtuihuo(Order order) {
		// TODO Auto-generated method stub
		return util.update("update t_order set o_tuihuo=? where o_id=?", order.getOtuihuo(), order.getOid());
	}

	@Override
	public boolean updateByOorder(Order order) {
		// TODO Auto-generated method stub
		return util.update("update t_order set o_order=? where o_id=?", order.getOorder(), order.getOid());
	}

	@Override
	public Order getShang(int id) {
		String sql = "SELECT *,DATE_FORMAT(o_date,'%Y-%m-%d %H:%i:%s') mytime from t_order where o_id<? ORDER BY o_id desc limit 1";
		CachedRowSet rs = util.query(sql, id);
		try {
			if (rs.next()) {
				Order o = new Order();
				o.setOid(rs.getInt("o_id"));
				o.setUid((rs.getInt("u_id")));
				o.setOprice(rs.getInt("o_price"));
				o.setOdate(rs.getString("mytime"));
				o.setOstate(rs.getInt("o_state"));
				o.setOsend(rs.getInt("o_send"));
				o.setOtuihuo(rs.getInt("o_tuihuo"));
				o.setOpayed(rs.getInt("o_payed"));
				o.setOnum(rs.getInt("o_num"));
				o.setOsuccess(rs.getInt("o_success"));
				o.setClothes(cd.queryByID(rs.getInt("c_id") + ""));
				o.setOorder(rs.getInt("o_order"));

				return o;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			util.closeCacheRowSet(rs);
		}
		return null;
	}

	@Override
	public Order getXia(int id) {
		String sql = "SELECT *,DATE_FORMAT(o_date,'%Y-%m-%d %H:%i:%s') mytime from t_order where o_id>?  limit 1";
		CachedRowSet rs = util.query(sql, id);
		try {
			if (rs.next()) {
				Order o = new Order();
				o.setOid(rs.getInt("o_id"));
				o.setUid((rs.getInt("u_id")));
				o.setOprice(rs.getInt("o_price"));
				o.setOdate(rs.getString("mytime"));
				o.setOstate(rs.getInt("o_state"));
				o.setOsend(rs.getInt("o_send"));
				o.setOtuihuo(rs.getInt("o_tuihuo"));
				o.setOpayed(rs.getInt("o_payed"));
				o.setOnum(rs.getInt("o_num"));
				o.setOsuccess(rs.getInt("o_success"));
				o.setClothes(cd.queryByID(rs.getInt("c_id") + ""));
				o.setOorder(rs.getInt("o_order"));
				return o;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			util.closeCacheRowSet(rs);
		}
		return null;
	}

	@Override
	public int getCount() {
		String sql = "select count(o_id) mycount from t_order";
		CachedRowSet rs = util.query(sql);
		try {
			if (rs.next()) {
				return rs.getInt("mycount");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			util.closeCacheRowSet(rs);
		}
		return -1;

	}

	@Override
	public List<Order> queryByPage(int page, int size) {
		// TODO Auto-generated method stub
		List<Order> list=new ArrayList<>();
		String sql="SELECT *,DATE_FORMAT(o_date,'%Y-%m-%d %H:%i:%s') mytime from t_order ORDER BY o_id desc limit "+(page-1)*size+" , "+size;
		CachedRowSet rs=util.query(sql);
		try {
			while(rs.next()) {
				Order o = new Order();
				o.setOid(rs.getInt("o_id"));
				o.setUid((rs.getInt("u_id")));
				o.setOprice(rs.getInt("o_price"));
				o.setOdate(rs.getString("mytime"));
				o.setOstate(rs.getInt("o_state"));
				o.setOsend(rs.getInt("o_send"));
				o.setOtuihuo(rs.getInt("o_tuihuo"));
				o.setOpayed(rs.getInt("o_payed"));
				o.setOnum(rs.getInt("o_num"));
				o.setOsuccess(rs.getInt("o_success"));
				o.setClothes(cd.queryByID(rs.getInt("c_id") + ""));
				o.setOorder(rs.getInt("o_order"));
				
				list.add(o);			
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			util.closeCacheRowSet(rs);
		}
		return list;
	}

}
